package com.zelin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zelin.pojo.R;
import com.zelin.pojo.Student;
import com.zelin.service.StudentService;
import com.zelin.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/10/19-15:52
 * ------------------------------
 */
@RestController
@RequestMapping("student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //1. 查询所有
    @GetMapping("/findAll")
    public List<Student> findAll(){
        return studentService.findAll();
    }
    //2. 分页查询
    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable int page,@PathVariable int pageSize){
       //2.1 得到分页结果
       Page<Student> studentPage =  studentService.findByPage(page,pageSize);
       //2.2 返回 对象
        return  R.ok()
                .data("total",studentPage.getTotal())
                .data("rows",studentPage.getRecords());
    }

    //3. 添加学生
    @PostMapping("/add")
    public R add(@RequestBody Student student){
        //3.1 添加学生
        studentService.insert(student);
        //3.2 添加成功后返回
        return R.ok().data("msg","添加成功!");
    }

    //4. 修改学生
    @PutMapping("/update/{sid}")
    public R update(@PathVariable int sid,@RequestBody Student student){
        //4.1 设置主键
        student.setSid(sid);
        //4.2 修改学生
        studentService.update(student);
        //4.3 返回
        return R.ok().data("msg","修改成功!");
    }

    //5. 删除学生
    @DeleteMapping("/{sid}")
    public R delete(@PathVariable int sid){
        //5.1 从数据库中删除学生
        studentService.delete(sid);
        //5.2 返回
        return R.ok().data("msg","删除成功!");
    }

    //6. 条件查询带分页
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable int page,@PathVariable int pageSize,@RequestBody StudentVo student){
        //6.1 开始查询
        Page<Student> studentPage = studentService.search(page,pageSize,student);
        //6.2 返回
        return R.ok().data("total",studentPage.getTotal()).data("rows",studentPage.getRecords());
    }
}
